﻿using Microsoft.AspNetCore.Http;
using System;
using System.Text.Json;
using Worldme.YHChatSDK.Framework.Vo.V1.Req;
using Worldme.YHChatSDK.Framework.Vo.V1.Ret;

namespace Worldme.YHChatSDK.Framework.Utils
{
    public class ApiUtil
    {
        private static string url = "https://chat-go.jwzhd.com/open-apis/v1/bot";

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="apiSendMsgReqV1"></param>
        /// <returns></returns>
        public static ApiSendMsgRetV1 SendMsg(ApiSendMsgReqV1 apiSendMsgReqV1)
        {
            string urlPost = url + "/send?token=" + YHChatSDKApplication.GetToken();
            string content = JsonSerializer.Serialize(apiSendMsgReqV1);
            string body = HttpUtil.Create().Post(urlPost).Header("Content-Type", "application/json; charset=utf-8").Body(content).Execute().Body();
            ApiSendMsgRetV1 apiSendMsgRetV1 = JsonSerializer.Deserialize<ApiSendMsgRetV1>(body);
            return apiSendMsgRetV1;
        }

        /// <summary>
        /// 批量发送消息
        /// </summary>
        /// <param name="apiSendMsgBatchReqV1"></param>
        /// <returns></returns>
        public static ApiSendMsgBatchRetV1 SendMsgBatch(ApiSendMsgBatchReqV1 apiSendMsgBatchReqV1)
        {
            string urlPost = url + "/batch_send?token=" + YHChatSDKApplication.GetToken();
            string content = JsonSerializer.Serialize(apiSendMsgBatchReqV1);
            string body = HttpUtil.Create().Post(urlPost).Header("Content-Type", "application/json; charset=utf-8").Body(content).Execute().Body();
            ApiSendMsgBatchRetV1 apiSendMsgBatchRetV1 = JsonSerializer.Deserialize<ApiSendMsgBatchRetV1>(body);
            return apiSendMsgBatchRetV1;
        }

        /// <summary>
        /// 编辑消息
        /// 参数中的recvId、recvType需要和原消息保持一致，否则无法编辑
        /// </summary>
        /// <param name="apiEditMsgReqV1"></param>
        /// <returns></returns>
        public static ApiEditMsgRetV1 EditMsg(ApiEditMsgReqV1 apiEditMsgReqV1)
        {
            string urlPost = url + "/edit?token=" + YHChatSDKApplication.GetToken();
            string content = JsonSerializer.Serialize(apiEditMsgReqV1);
            string body = HttpUtil.Create().Post(urlPost).Header("Content-Type", "application/json; charset=utf-8").Body(content).Execute().Body();
            ApiEditMsgRetV1 apiEditMsgRetV1 = JsonSerializer.Deserialize<ApiEditMsgRetV1>(body);
            return apiEditMsgRetV1;
        }

        /// <summary>
        /// 撤回消息
        /// 参数中的chatId、chatType需要和原消息保持一致，否则无法撤回
        /// </summary>
        /// <param name="apiRecallMsgReqV1"></param>
        /// <returns></returns>
        public static ApiRecallMsgRetV1 RecallMsg(ApiRecallMsgReqV1 apiRecallMsgReqV1)
        {
            string urlPost = url + "/recall?token=" + YHChatSDKApplication.GetToken();
            string content = JsonSerializer.Serialize(apiRecallMsgReqV1);
            string body = HttpUtil.Create().Post(urlPost).Header("Content-Type", "application/json; charset=utf-8").Body(content).Execute().Body();
            ApiRecallMsgRetV1 apiRecallMsgRetV1 = JsonSerializer.Deserialize<ApiRecallMsgRetV1>(body);
            return apiRecallMsgRetV1;
        }

        /// <summary>
        /// 设置看板
        /// </summary>
        /// <param name="apiSetBoardReqV1"></param>
        /// <returns></returns>
        public static ApiSetBoardRetV1 SetBoard(ApiSetBoardReqV1 apiSetBoardReqV1)
        {
            String urlPost;
            if (string.IsNullOrEmpty(apiSetBoardReqV1.chatId) && string.IsNullOrEmpty(apiSetBoardReqV1.chatType))
            {
                urlPost = url + "/board-all?token=" + YHChatSDKApplication.GetToken();
            }
            else
            {
                urlPost = url + "/board?token=" + YHChatSDKApplication.GetToken();
            }
            String content = JsonSerializer.Serialize(apiSetBoardReqV1);
            String body = HttpUtil.Create().Post(urlPost).Header("Content-Type", "application/json; charset=utf-8").Body(content).Execute().Body();
            ApiSetBoardRetV1 apiSetBoardRetV1 = JsonSerializer.Deserialize<ApiSetBoardRetV1>(body);
            return apiSetBoardRetV1;
        }

        /// <summary>
        /// 取消设置看板
        /// </summary>
        /// <param name="apiDisBoardReqV1"></param>
        /// <returns></returns>
        public static ApiDisBoardRetV1 DisBoard(ApiDisBoardReqV1 apiDisBoardReqV1)
        {
            String urlPost;
            if (string.IsNullOrEmpty(apiDisBoardReqV1.chatId) && string.IsNullOrEmpty(apiDisBoardReqV1.chatType))
            {
                urlPost = url + "/board-all-dismiss?token=" + YHChatSDKApplication.GetToken();
            }
            else
            {
                urlPost = url + "/board-dismiss?token=" + YHChatSDKApplication.GetToken();
            }
            String content = JsonSerializer.Serialize(apiDisBoardReqV1);
            String body = HttpUtil.Create().Post(urlPost).Header("Content-Type", "application/json; charset=utf-8").Body(content).Execute().Body();
            ApiDisBoardRetV1 apiDisBoardRetV1 = JsonSerializer.Deserialize<ApiDisBoardRetV1>(body);
            return apiDisBoardRetV1;
        }



    }
}
